/**
 * 
 */
package com.example.radioclub.login.yahoo;

import java.util.Map;

import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;

import com.example.radioclub.data.Constants;

/**
 * Utilities for logging in with Yahoo Account are defined here and please add another
 * utilities as much as you need to here. Just to make things properly arranged.
 * 
 * @author wizard
 *
 */
public class YahooLoginUtils {
	public static String createGetRequestTokenUrl(String oauth_nonce, String oauth_timestamp, 
			String oauth_consumer_key, String oauth_signature_method, String oauth_signature, 
			String oauth_version, String oauth_pref_language, String oauth_callback) {
		return Constants.API_YAHOO_REQUEST_TOKEN 
				+ "?oauth_nonce=" + oauth_nonce 
				+ "&oauth_timestamp=" + oauth_timestamp 
				+ "&oauth_consumer_key=" + oauth_consumer_key
				+ "&oauth_signature_method=" + oauth_signature_method 
				+ "&oauth_signature=" + oauth_signature
				+ "&oauth_version=" + oauth_version
				+ "&xoauth_lang_pref=" + oauth_pref_language
				+ "&oauth_callback=" + oauth_callback;
	}
	
	public static MultiValueMap<String, String> extractToken(String result){
		MultiValueMap<String, String> token = new LinkedMultiValueMap<String, String>();
		String[] array = StringUtils.split(result, "?");
		array = StringUtils.split(array[1], "&");
		for(int i=0; i<array.length; ++i){
			String[] s = StringUtils.split(array[i], "=");
			token.add(s[0], s[1]);
		}
		return token;
	}
	
	public static String getToken(String result, String tokenName){
		return extractToken(result).get(tokenName).get(0);
	}
	
	public static String createGetUserAuthentication(String oauth_token) {
		return Constants.API_YAHOO_REQUEST_AUTH + "?oauth_token=" + oauth_token;
	}
	
}

